package repository;

import annotation.DependencyInjection;
import annotation.Singleton;
import database.GroupMessageDatabase;
import entity.GroupMessage;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Singleton
public class GroupMessageRepository {
    private GroupMessageDatabase database;

    @DependencyInjection
    public GroupMessageRepository(GroupMessageDatabase database) {
        this.database = database;
    }

    public void startWrite() {
        database.startWrite();
    }

    public void endWrite() {
        database.endWrite();
    }

    public void startRead() {
        database.startRead();
    }

    public void endRead() {
        database.endRead();
    }

    public List<GroupMessage> getByUuid(UUID groupId) {
        return database.getList().stream().filter(groupMessage -> groupMessage.getGroupId().equals(groupId)).map(GroupMessage::new).collect(Collectors.toList());
    }

    public List<GroupMessage> get(UUID groupId, LocalDateTime left, LocalDateTime rightEqual) {
        return database.getList().stream().filter(groupMessage -> groupMessage.getGroupId().equals(groupId) && groupMessage.getTime().isAfter(left) && !groupMessage.getTime().isAfter(rightEqual)).map(GroupMessage::new).collect(Collectors.toList());
    }

    public void add(GroupMessage groupMessage) {
        database.getList().add(groupMessage);
    }
}
